
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>网络编程 · Dart编程指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="arcticfox">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="爬虫实战.html" />
    
    
    <link rel="prev" href="实战篇.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.bczl.xyz/" target="_blank" class="custom-link">主页</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础篇.html">
            
                <a href="基础篇.html">
            
                    
                    基础篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="环境搭建.html">
            
                <a href="环境搭建.html">
            
                    
                    环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="简单语法.html">
            
                <a href="简单语法.html">
            
                    
                    简单语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="进阶篇.html">
            
                <a href="进阶篇.html">
            
                    
                    进阶篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="泛型.html">
            
                <a href="泛型.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="枚举.html">
            
                <a href="枚举.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="库与导入.html">
            
                <a href="库与导入.html">
            
                    
                    库与导入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="异步编程.html">
            
                <a href="异步编程.html">
            
                    
                    异步编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.5.1" data-path="异步编程之Future.html">
            
                <a href="异步编程之Future.html">
            
                    
                    Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.2" data-path="异步编程之Stream.html">
            
                <a href="异步编程之Stream.html">
            
                    
                    Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.3" data-path="异步编程之Isolate.html">
            
                <a href="异步编程之Isolate.html">
            
                    
                    Isolate
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理与 Zone
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="生成器.html">
            
                <a href="生成器.html">
            
                    
                    生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="标准流与文件.html">
            
                <a href="标准流与文件.html">
            
                    
                    标准流与文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="运算符.html">
            
                <a href="运算符.html">
            
                    
                    附：运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="常用标准库.html">
            
                <a href="常用标准库.html">
            
                    
                    常用标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="集合.html">
            
                <a href="集合.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="URI处理.html">
            
                <a href="URI处理.html">
            
                    
                    URI处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="日期和时间.html">
            
                <a href="日期和时间.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="环境及外部调用.html">
            
                <a href="环境及外部调用.html">
            
                    
                    环境及外部调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="其他.html">
            
                <a href="其他.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="实战篇.html">
            
                <a href="实战篇.html">
            
                    
                    实战篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.5.1" data-path="网络编程.html">
            
                <a href="网络编程.html">
            
                    
                    网络编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="爬虫实战.html">
            
                <a href="爬虫实战.html">
            
                    
                    爬虫实战
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="HTTP服务器.html">
            
                <a href="HTTP服务器.html">
            
                    
                    HTTP服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="WebSocket服务器.html">
            
                <a href="WebSocket服务器.html">
            
                    
                    WebSocket服务器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    附录
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="dart2.14.html">
            
                <a href="dart2.14.html">
            
                    
                    升级到Dart 2.14
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >网络编程</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#&#x7F51;&#x7EDC;&#x7F16;&#x7A0B;"><b></b>&#x7F51;&#x7EDC;&#x7F16;&#x7A0B;</a></li><ul><li><span class="title-icon "></span><a href="#cs&#x67B6;&#x6784;"><b></b>C/S&#x67B6;&#x6784;</a></li><li><span class="title-icon "></span><a href="#tcp-&#x670D;&#x52A1;&#x7AEF;"><b></b>TCP &#x670D;&#x52A1;&#x7AEF;</a></li><li><span class="title-icon "></span><a href="#tcp-&#x5BA2;&#x6237;&#x7AEF;"><b></b>TCP &#x5BA2;&#x6237;&#x7AEF;</a></li><li><span class="title-icon "></span><a href="#udp-&#x670D;&#x52A1;&#x7AEF;"><b></b>UDP &#x670D;&#x52A1;&#x7AEF;</a></li><li><span class="title-icon "></span><a href="#udp-&#x5BA2;&#x6237;&#x7AEF;"><b></b>UDP &#x5BA2;&#x6237;&#x7AEF;</a></li><li><span class="title-icon "></span><a href="#http&#x670D;&#x52A1;&#x5668;&#x4E0E;&#x8BF7;&#x6C42;"><b></b>HTTP&#x670D;&#x52A1;&#x5668;&#x4E0E;&#x8BF7;&#x6C42;</a></li><li><span class="title-icon "></span><a href="#&#x3000;websocket"><b></b>&#x3000;WebSocket</a></li></ul></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><!-- ex_nolevel -->
<h2 id="&#x7F51;&#x7EDC;&#x7F16;&#x7A0B;"><a name="&#x7F51;&#x7EDC;&#x7F16;&#x7A0B;" class="anchor-navigation-ex-anchor" href="#&#x7F51;&#x7EDC;&#x7F16;&#x7A0B;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7F51;&#x7EDC;&#x7F16;&#x7A0B;</h2>
<p>&#x901A;&#x5E38;&#x7F51;&#x7EDC;&#x7F16;&#x7A0B;&#x4E5F;&#x88AB;&#x79F0;&#x4E3A;<code>&#x5957;&#x63A5;&#x5B57;</code>&#xFF08;socket&#xFF09;&#x7F16;&#x7A0B;&#xFF0C;&#x5B83;&#x6700;&#x65E9;&#x53EF;&#x4EE5;&#x8FFD;&#x6EAF;&#x5230; 20 &#x4E16;&#x7EAA; 70 &#x5E74;&#x4EE3;&#xFF0C;&#x5728;&#x7F8E;&#x56FD;&#x52A0;&#x5229;&#x798F;&#x5C3C;&#x4E9A;&#x5927;&#x5B66;&#x7684;&#x4F2F;&#x514B;&#x5229;&#x7248;&#x672C; UNIX&#x7CFB;&#x7EDF;&#xFF08;&#x5373;BSD UNIX&#xFF09;&#x4E0A;&#x51FA;&#x73B0;&#x3002;</p>
<h3 id="cs&#x67B6;&#x6784;"><a name="cs&#x67B6;&#x6784;" class="anchor-navigation-ex-anchor" href="#cs&#x67B6;&#x6784;"><i class="fa fa-link" aria-hidden="true"></i></a>C/S&#x67B6;&#x6784;</h3>
<p>C&#x6307;<code>Client</code>&#x5373;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;S&#x6307;<code>Server</code> &#x5373;&#x670D;&#x52A1;&#x7AEF;&#x3002;C/S&#x67B6;&#x6784;&#x4E5F;&#x5C31;&#x662F;&#x6240;&#x8C13;&#x7684;&#x5BA2;&#x6237;&#x7AEF;/&#x670D;&#x52A1;&#x7AEF;&#x67B6;&#x6784;&#x3002;&#x7B80;&#x5355;&#x6765;&#x8BF4;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x4E5F;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x7528;&#x6237;&#x7535;&#x8111;&#x4E0A;&#x7684;&#x7A0B;&#x5E8F;&#xFF0C;&#x800C;&#x670D;&#x52A1;&#x7AEF;&#x5219;&#x662F;&#x8FDC;&#x7A0B;&#x7684;&#x7528;&#x4E8E;&#x63A5;&#x6536;&#x3001;&#x5904;&#x7406;&#x6765;&#x81EA;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&#x7684;&#x7A0B;&#x5E8F;&#x3002;</p>
<h3 id="tcp-&#x670D;&#x52A1;&#x7AEF;"><a name="tcp-&#x670D;&#x52A1;&#x7AEF;" class="anchor-navigation-ex-anchor" href="#tcp-&#x670D;&#x52A1;&#x7AEF;"><i class="fa fa-link" aria-hidden="true"></i></a>TCP &#x670D;&#x52A1;&#x7AEF;</h3>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:convert&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  <span class="hljs-comment">//&#x7ED1;&#x5B9A;&#x672C;&#x5730;localhost&#x7684;8081&#x7AEF;&#x53E3;(&#x5373;&#x7ED1;&#x5B9A;&#xFF1A;127.0.0.1)</span>
  ServerSocket.bind(InternetAddress.loopbackIPv4, <span class="hljs-number">8081</span>)
  .then((serverSocket) {
    serverSocket.listen((socket) {
      socket.cast&lt;<span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">int</span>&gt;&gt;().transform(utf8.decoder).listen(<span class="hljs-built_in">print</span>);
    });
  });
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4E3A;&#x7B80;&#x6D01;&#x793A;&#x4F8B;&#xFF0C;&#x4E0D;&#x662F;&#x975E;&#x5E38;&#x6E05;&#x6670;&#xFF0C;&#x7B49;&#x4EF7;&#x4E8E;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:convert&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  startTCPServer();
}

<span class="hljs-comment">// TCP &#x670D;&#x52A1;&#x7AEF;</span>
<span class="hljs-keyword">void</span> startTCPServer() <span class="hljs-keyword">async</span>{
  ServerSocket serverSocket = <span class="hljs-keyword">await</span> ServerSocket.bind(InternetAddress.loopbackIPv4, <span class="hljs-number">8081</span>);

  <span class="hljs-comment">//&#x904D;&#x5386;&#x6240;&#x6709;&#x8FDE;&#x63A5;&#x5230;&#x670D;&#x52A1;&#x5668;&#x7684;&#x5957;&#x63A5;&#x5B57;</span>
  <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span>(Socket socket <span class="hljs-keyword">in</span> serverSocket) {
    <span class="hljs-comment">// &#x5148;&#x5C06;&#x5B57;&#x8282;&#x6D41;&#x4EE5;utf-8&#x8FDB;&#x884C;&#x89E3;&#x7801;</span>
    socket.cast&lt;<span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">int</span>&gt;&gt;().transform(utf8.decoder).listen((data) {
      <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;from <span class="hljs-subst">${socket.remoteAddress.address}</span> data:&quot;</span> + data);
      socket.add(utf8.encode(<span class="hljs-string">&apos;hello client!&apos;</span>));
    });
  }
}
</code></pre>
<h3 id="tcp-&#x5BA2;&#x6237;&#x7AEF;"><a name="tcp-&#x5BA2;&#x6237;&#x7AEF;" class="anchor-navigation-ex-anchor" href="#tcp-&#x5BA2;&#x6237;&#x7AEF;"><i class="fa fa-link" aria-hidden="true"></i></a>TCP &#x5BA2;&#x6237;&#x7AEF;</h3>
<p>&#x5BF9;&#x5E94;&#x7684;&#x7B80;&#x6D01;&#x8868;&#x8FBE;&#x5982;&#x4E0B;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:convert&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  <span class="hljs-comment">// &#x8FDE;&#x63A5;127.0.0.1&#x7684;8081&#x7AEF;&#x53E3;</span>
  Socket.connect(<span class="hljs-string">&apos;127.0.0.1&apos;</span>, <span class="hljs-number">8081</span>).then((socket) {
    socket.write(<span class="hljs-string">&apos;Hello, Server!&apos;</span>);
    socket.cast&lt;<span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">int</span>&gt;&gt;().transform(utf8.decoder).listen(<span class="hljs-built_in">print</span>);
  });
}
</code></pre>
<p>&#x66F4;&#x6E05;&#x6670;&#x5199;&#x6CD5;&#x5982;&#x4E0B;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:convert&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  startTCPClient();
}

<span class="hljs-comment">// TCP &#x5BA2;&#x6237;&#x7AEF;</span>
<span class="hljs-keyword">void</span> startTCPClient() <span class="hljs-keyword">async</span> {
  <span class="hljs-comment">//&#x8FDE;&#x63A5;&#x670D;&#x52A1;&#x5668;&#x7684;8081&#x7AEF;&#x53E3;</span>
  Socket socket = <span class="hljs-keyword">await</span> Socket.connect(<span class="hljs-string">&apos;127.0.0.1&apos;</span>, <span class="hljs-number">8081</span>);
  socket.write(<span class="hljs-string">&apos;Hello, Server!&apos;</span>);
  socket.cast&lt;<span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">int</span>&gt;&gt;().transform(utf8.decoder).listen(<span class="hljs-built_in">print</span>);
}
</code></pre>
<h3 id="udp-&#x670D;&#x52A1;&#x7AEF;"><a name="udp-&#x670D;&#x52A1;&#x7AEF;" class="anchor-navigation-ex-anchor" href="#udp-&#x670D;&#x52A1;&#x7AEF;"><i class="fa fa-link" aria-hidden="true"></i></a>UDP &#x670D;&#x52A1;&#x7AEF;</h3>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:convert&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  startUDPServer();
}

<span class="hljs-comment">// UDP &#x670D;&#x52A1;&#x7AEF;</span>
<span class="hljs-keyword">void</span> startUDPServer() <span class="hljs-keyword">async</span> {
  RawDatagramSocket rawDgramSocket = <span class="hljs-keyword">await</span> RawDatagramSocket.bind(InternetAddress.loopbackIPv4, <span class="hljs-number">8081</span>);

  <span class="hljs-comment">//&#x76D1;&#x542C;&#x5957;&#x63A5;&#x5B57;&#x4E8B;&#x4EF6;</span>
  <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span> (RawSocketEvent event <span class="hljs-keyword">in</span> rawDgramSocket) {
    <span class="hljs-comment">// &#x6570;&#x636E;&#x5305;&#x5957;&#x63A5;&#x5B57;&#x4E0D;&#x80FD;&#x76D1;&#x542C;&#x6570;&#x636E;&#xFF0C;&#x800C;&#x662F;&#x76D1;&#x542C;&#x4E8B;&#x4EF6;&#x3002;</span>
    <span class="hljs-comment">// &#x5F53;&#x4E8B;&#x4EF6;&#x4E3A;RawSocketEvent.read&#x7684;&#x65F6;&#x5019;&#x624D;&#x80FD;&#x901A;&#x8FC7;receive&#x51FD;&#x6570;&#x63A5;&#x6536;&#x6570;&#x636E;</span>
    <span class="hljs-keyword">if</span>(event == RawSocketEvent.read) {
        <span class="hljs-built_in">print</span>(utf8.decode(rawDgramSocket.receive().data));
        rawDgramSocket.send(utf8.encode(<span class="hljs-string">&quot;UDP Server:already received!&quot;</span>), InternetAddress.loopbackIPv4, <span class="hljs-number">8082</span>);
      }
  }
}
</code></pre>
<h3 id="udp-&#x5BA2;&#x6237;&#x7AEF;"><a name="udp-&#x5BA2;&#x6237;&#x7AEF;" class="anchor-navigation-ex-anchor" href="#udp-&#x5BA2;&#x6237;&#x7AEF;"><i class="fa fa-link" aria-hidden="true"></i></a>UDP &#x5BA2;&#x6237;&#x7AEF;</h3>
<pre><code class="lang-dart">
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:convert&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  startUDPClent();
}

<span class="hljs-comment">// UDP &#x5BA2;&#x6237;&#x7AEF;</span>
<span class="hljs-keyword">void</span> startUDPClent() <span class="hljs-keyword">async</span> {
   RawDatagramSocket rawDgramSocket = <span class="hljs-keyword">await</span> RawDatagramSocket.bind(<span class="hljs-string">&apos;127.0.0.1&apos;</span>, <span class="hljs-number">8082</span>);

   rawDgramSocket.send(utf8.encode(<span class="hljs-string">&quot;hello,world!&quot;</span>), InternetAddress(<span class="hljs-string">&apos;127.0.0.1&apos;</span>), <span class="hljs-number">8081</span>);

  <span class="hljs-comment">//&#x76D1;&#x542C;&#x5957;&#x63A5;&#x5B57;&#x4E8B;&#x4EF6;</span>
  <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span> (RawSocketEvent event <span class="hljs-keyword">in</span> rawDgramSocket) {
    <span class="hljs-keyword">if</span>(event == RawSocketEvent.read) {
        <span class="hljs-comment">// &#x63A5;&#x6536;&#x6570;&#x636E;</span>
        <span class="hljs-built_in">print</span>(utf8.decode(rawDgramSocket.receive().data));
      }
  }
}
</code></pre>
<h3 id="http&#x670D;&#x52A1;&#x5668;&#x4E0E;&#x8BF7;&#x6C42;"><a name="http&#x670D;&#x52A1;&#x5668;&#x4E0E;&#x8BF7;&#x6C42;" class="anchor-navigation-ex-anchor" href="#http&#x670D;&#x52A1;&#x5668;&#x4E0E;&#x8BF7;&#x6C42;"><i class="fa fa-link" aria-hidden="true"></i></a>HTTP&#x670D;&#x52A1;&#x5668;&#x4E0E;&#x8BF7;&#x6C42;</h3>
<p>&#x5173;&#x4E8E;HTTP&#x7684;&#x8BE6;&#x7EC6;&#x8BF4;&#x660E;&#xFF0C;&#x53EF;&#x53C2;&#x52A0;&#x672C;&#x4EBA; <a href="https://blog.csdn.net/yingshukun/category_9281313.html" target="_blank">&#x76F8;&#x5173;&#x535A;&#x5BA2;</a></p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  HttpServer
      .bind(InternetAddress.loopbackIPv4, <span class="hljs-number">8080</span>)
      .then((server) {
        server.listen((HttpRequest request) {
          <span class="hljs-built_in">print</span>(request.uri.path);
          <span class="hljs-keyword">if</span>(request.uri.path.startsWith(<span class="hljs-string">&quot;/greet&quot;</span>)){
            <span class="hljs-keyword">var</span> subPathList = request.uri.path.split(<span class="hljs-string">&quot;/&quot;</span>);
            <span class="hljs-built_in">print</span>(subPathList);
            <span class="hljs-keyword">if</span>(subPathList.length &gt;=<span class="hljs-number">3</span>){
              request.response.write(<span class="hljs-string">&apos;Hello, <span class="hljs-subst">${subPathList[<span class="hljs-number">2</span>]}</span>&apos;</span>);
              request.response.close();
            }<span class="hljs-keyword">else</span>{
             request.response.write(<span class="hljs-string">&apos;Hello, &apos;</span>);
             request.response.close();
            }
          }<span class="hljs-keyword">else</span>{
            request.response.write(<span class="hljs-string">&apos;Welcome to test server!&apos;</span>);
            request.response.close();
          }
        });
      });
}
</code></pre>
<p>&#x6D4F;&#x89C8;&#x5668;&#x8F93;&#x5165;<code>http://localhost:8080/greet/zhangsan</code>&#x8BBF;&#x95EE;</p>
<p>&#x4EE5;&#x4E0A;&#x662F;&#x4F7F;&#x7528;&#x6D4F;&#x89C8;&#x5668;&#x5411;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4EE3;&#x7801;&#x6A21;&#x62DF;&#x6D4F;&#x89C8;&#x5668;&#x53D1;&#x8BF7;&#x6C42;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:convert&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  HttpClient client = HttpClient();

  client.getUrl(<span class="hljs-built_in">Uri</span>.parse(<span class="hljs-string">&quot;https://www.baidu.com/&quot;</span>))
    .then((HttpClientRequest request) {
      <span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x8BF7;&#x6C42;&#x5934;</span>
      request.headers.add(HttpHeaders.userAgentHeader,
      <span class="hljs-string">&quot;Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36&quot;</span>);
      <span class="hljs-keyword">return</span> request.close();
    })
    .then((HttpClientResponse response) {
      <span class="hljs-comment">// &#x5904;&#x7406;&#x54CD;&#x5E94;</span>
      response.transform(utf8.decoder).listen((contents) {
        <span class="hljs-built_in">print</span>(contents);
      });
    });
}
</code></pre>
<p>&#x901A;&#x5E38;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x5E76;&#x4E0D;&#x4F1A;&#x76F4;&#x63A5;&#x4F7F;&#x7528;Dart &#x6807;&#x51C6;&#x5E93;&#x63D0;&#x4F9B;&#x7684;<code>http</code> &#x7F51;&#x7EDC;&#x8BF7;&#x6C42;API&#xFF0C;&#x56E0;&#x4E3A;&#x6807;&#x51C6;&#x5E93;&#x5E93;&#x4F7F;&#x7528;&#x4E0A;&#x4ECD;&#x7136;&#x8FC7;&#x4E8E;&#x7E41;&#x7410;&#xFF0C;&#x7B2C;&#x4E09;&#x65B9;&#x5E93;&#x5219;&#x66F4;&#x52A0;&#x7B80;&#x6D01;&#x5F3A;&#x5927;&#x3002;&#x5728;Flutter&#x4E0A;&#xFF0C;&#x4E3B;&#x8981;&#x4F7F;&#x7528;<code>dio</code>&#x5E93;&#xFF0C;&#x529F;&#x80FD;&#x5341;&#x5206;&#x5F3A;&#x5927;&#xFF0C;&#x53E6;&#x5916;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B98;&#x65B9;&#x63A8;&#x51FA;&#x7684;<code>http</code>&#x5E93;&#xFF0C;&#x66F4;&#x52A0;&#x7B80;&#x6D01;&#x7CBE;&#x70BC;&#xFF0C;&#x94FE;&#x63A5;&#x5982;&#x4E0B;</p>
<ul>
<li><a href="https://pub.dev/packages/http" target="_blank">http</a></li>
<li><a href="https://pub.dev/packages/dio" target="_blank">dio</a></li>
</ul>
<h3 id="&#x3000;websocket"><a name="&#x3000;websocket" class="anchor-navigation-ex-anchor" href="#&#x3000;websocket"><i class="fa fa-link" aria-hidden="true"></i></a>&#x3000;WebSocket</h3>
<blockquote>
<p><strong>WebSocket</strong>&#x662F;&#x4E00;&#x79CD;&#x5728;&#x5355;&#x4E2A;<a href="https://baike.baidu.com/item/TCP" target="_blank">TCP</a>&#x8FDE;&#x63A5;&#x4E0A;&#x8FDB;&#x884C;<a href="https://baike.baidu.com/item/%E5%85%A8%E5%8F%8C%E5%B7%A5" target="_blank">&#x5168;&#x53CC;&#x5DE5;</a>&#x901A;&#x4FE1;&#x7684;&#x534F;&#x8BAE;&#x3002;&#x5B83;&#x7684;&#x51FA;&#x73B0;&#x4F7F;&#x5F97;&#x5BA2;&#x6237;&#x7AEF;&#x548C;&#x670D;&#x52A1;&#x7AEF;&#x90FD;&#x53EF;&#x4EE5;&#x4E3B;&#x52A8;&#x7684;&#x63A8;&#x9001;&#x6D88;&#x606F;&#xFF0C;&#x53EF;&#x4EE5;&#x662F;&#x6587;&#x672C;&#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x3002;&#x800C;&#x4E14;&#x6CA1;&#x6709;&#x540C;&#x6E90;&#x7B56;&#x7565;&#x7684;&#x9650;&#x5236;&#xFF0C;&#x4E0D;&#x5B58;&#x5728;&#x8DE8;&#x57DF;&#x95EE;&#x9898;&#x3002;&#x534F;&#x8BAE;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#x5C31;&#x662F;<code>ws</code>&#x3002;&#x50CF;https&#x4E00;&#x6837;&#x5982;&#x679C;&#x52A0;&#x5BC6;&#x7684;&#x8BDD;&#x5C31;&#x662F;<code>wxs</code>&#x3002;</p>
</blockquote>
<p>WebSocket &#x662F;&#x72EC;&#x7ACB;&#x7684;&#x3001;&#x521B;&#x5EFA;&#x5728; TCP &#x4E0A;&#x7684;&#x534F;&#x8BAE;&#x3002;</p>
<p>Websocket &#x901A;&#x8FC7;<a href="https://baike.baidu.com/item/HTTP" target="_blank">HTTP</a>/1.1 &#x534F;&#x8BAE;&#x7684;101&#x72B6;&#x6001;&#x7801;&#x8FDB;&#x884C;&#x63E1;&#x624B;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x521B;&#x5EFA;Websocket&#x8FDE;&#x63A5;&#xFF0C;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x6D4F;&#x89C8;&#x5668;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#xFF0C;&#x4E4B;&#x540E;&#x670D;&#x52A1;&#x5668;&#x8FDB;&#x884C;&#x56DE;&#x5E94;&#xFF0C;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#x901A;&#x5E38;&#x79F0;&#x4E3A;&#x201C;&#x63E1;&#x624B;&#x201D;&#xFF08;handshaking&#xFF09;</p>
<h4 id="&#x3000;&#x670D;&#x52A1;&#x7AEF;"><a name="&#x3000;&#x670D;&#x52A1;&#x7AEF;" class="anchor-navigation-ex-anchor" href="#&#x3000;&#x670D;&#x52A1;&#x7AEF;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x3000;&#x670D;&#x52A1;&#x7AEF;</h4>
<p>Web&#x5957;&#x63A5;&#x5B57;&#x670D;&#x52A1;&#x5668;&#x4F7F;&#x7528;&#x666E;&#x901A;&#x7684;HTTP&#x670D;&#x52A1;&#x5668;&#x6765;&#x63A5;&#x53D7;Web&#x5957;&#x63A5;&#x5B57;&#x8FDE;&#x63A5;&#x3002;&#x521D;&#x59CB;&#x63E1;&#x624B;&#x662F;HTTP&#x8BF7;&#x6C42;&#xFF0C;&#x7136;&#x540E;&#x5C06;&#x5176;&#x5347;&#x7EA7;&#x4E3A;Web&#x5957;&#x63A5;&#x5B57;&#x8FDE;&#x63A5;&#x3002;&#x670D;&#x52A1;&#x5668;&#x4F7F;&#x7528;<a href="https://api.dart.dev/stable/2.7.1/dart-io/WebSocketTransformer-class.html" target="_blank">WebSocketTransformer</a>&#x5347;&#x7EA7;&#x8BF7;&#x6C42;&#xFF0C; &#x5E76;&#x4FA6;&#x542C;&#x8FD4;&#x56DE;&#x7684;Web&#x5957;&#x63A5;&#x5B57;&#x4E0A;&#x7684;&#x6570;&#x636E;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span> {
  HttpServer server = <span class="hljs-keyword">await</span> HttpServer.bind(InternetAddress.loopbackIPv4, <span class="hljs-number">8083</span>);
  <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span> (HttpRequest req <span class="hljs-keyword">in</span> server) {
    <span class="hljs-keyword">if</span> (req.uri.path == <span class="hljs-string">&apos;/ws&apos;</span>) {
      <span class="hljs-comment">// &#x5C06;&#x4E00;&#x4E2A;HttpRequest&#x8F6C;&#x4E3A;WebSocket&#x8FDE;&#x63A5;</span>
      WebSocket socket = <span class="hljs-keyword">await</span> WebSocketTransformer.upgrade(req);
      socket.listen((data) {
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;from IP <span class="hljs-subst">${req.connectionInfo.remoteAddress.address}</span>:<span class="hljs-subst">${data}</span>&quot;</span>);
        socket.add(<span class="hljs-string">&quot;WebSocket Server:already received!&quot;</span>);
      });
    }
  }
}
</code></pre>
<h4 id="&#x5BA2;&#x6237;&#x7AEF;"><a name="&#x5BA2;&#x6237;&#x7AEF;" class="anchor-navigation-ex-anchor" href="#&#x5BA2;&#x6237;&#x7AEF;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5BA2;&#x6237;&#x7AEF;</h4>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span> {
  WebSocket socket = <span class="hljs-keyword">await</span> WebSocket.connect(<span class="hljs-string">&apos;ws://127.0.0.1:8083/ws&apos;</span>);
  socket.add(<span class="hljs-string">&apos;Hello, World!&apos;</span>);

  <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> data <span class="hljs-keyword">in</span> socket) {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;from Server: $data&quot;</span>);

    <span class="hljs-comment">// &#x5173;&#x95ED;&#x8FDE;&#x63A5;</span>
    socket.close();
  }
}
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x672C;&#x7BC7;&#x5185;&#x5BB9;&#x4E3B;&#x8981;&#x4E3A;Dart&#x7F16;&#x7A0B;&#x793A;&#x4F8B;&#xFF0C;&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x8FD8;&#x6709;&#x8BB8;&#x591A;&#x95EE;&#x9898;&#x9700;&#x8981;&#x5904;&#x7406;&#xFF0C;&#x4F8B;&#x5982;TCP&#x7684;&#x7C98;&#x5305;&#x95EE;&#x9898;&#xFF0C;&#x5FC3;&#x8DF3;&#x673A;&#x5236;&#xFF0C;&#x5E76;&#x5728;Dart&#x4E2D;&#x5C06;WebSocket&#x7ED3;&#x5408;ProtoBuf&#x4F7F;&#x7528;&#x7B49;&#xFF0C;&#x76F8;&#x5173;&#x5185;&#x5BB9;&#x8BF7;&#x5173;&#x6CE8;&#x540E;&#x7EED;&#x7684;Flutter&#x9879;&#x76EE;&#x5B9E;&#x6218;&#x8BFE;&#x7A0B;&#x3002;</p>
<hr>
<p><strong>&#x516C;&#x4F17;&#x53F7;&#x201C;&#x7F16;&#x7A0B;&#x4E4B;&#x8DEF;&#x4ECE;0&#x5230;1&#x201D;</strong></p>
<p><img src="https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/20190301102949549.jpg" alt="20190301102949549"> </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; Arcticfox 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;
2021-03-18 13:34:19
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="实战篇.html" class="navigation navigation-prev " aria-label="Previous page: 实战篇">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="爬虫实战.html" class="navigation navigation-next " aria-label="Next page: 爬虫实战">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"网络编程","level":"1.5.1","depth":2,"next":{"title":"爬虫实战","level":"1.5.2","depth":2,"path":"爬虫实战.md","ref":"爬虫实战.md","articles":[]},"previous":{"title":"实战篇","level":"1.5","depth":1,"path":"实战篇.md","ref":"实战篇.md","articles":[{"title":"网络编程","level":"1.5.1","depth":2,"path":"网络编程.md","ref":"网络编程.md","articles":[]},{"title":"爬虫实战","level":"1.5.2","depth":2,"path":"爬虫实战.md","ref":"爬虫实战.md","articles":[]},{"title":"HTTP服务器","level":"1.5.3","depth":2,"path":"HTTP服务器.md","ref":"HTTP服务器.md","articles":[]},{"title":"WebSocket服务器","level":"1.5.4","depth":2,"path":"WebSocket服务器.md","ref":"WebSocket服务器.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["anchor-navigation-ex","expandable-chapters-small","-lunr","-search","search-pro","-sharing","github","expandable-chapters","pageview-count","theme-comscore","tbfed-pagefooter","popup","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy Arcticfox 2020","modify_label":"文档修订于：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"popup":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"favicon":"logo.ico","theme-comscore":{},"pageview-count":{},"custom-favicon":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"arcticfox","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Dart编程指南","language":"zh-hans","links":{"sidebar":{"主页":"http://www.bczl.xyz/"}},"gitbook":"*"},"file":{"path":"网络编程.md","mtime":"2021-03-18T05:34:19.136Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-10-18T14:58:03.414Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

